﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 药品信息管理
{
    public partial class YP_CRKMX : Form
    {
        public YP_CRKMX()
        {
            InitializeComponent();
        }

        private void buttonItem1_Click(object sender, EventArgs e)
        {
            db db1 = new db();
            string time1 = db1.datetoint(dateTimePicker1.Value.ToString("yyyy.MM.dd")).ToString(), time2 =db1.datetoint( dateTimePicker2.Value.ToString("yyyy.MM.dd")).ToString();
            string string_jb = " 药品级别 in ('') ", string_jx = " and 剂型 in ('') ";

            foreach (Control aa in panelEx1.Controls)
            {
                CheckBox cb = (CheckBox)aa;
                if (cb.Checked == true)
                {
                    string_jb = string_jb.Replace("''", "'','" + cb.Text + "'");
                }

            }


            foreach (Control aa in panelEx2.Controls)
            {
                CheckBox cb = (CheckBox)aa;
                if (cb.Checked == true)
                {
                    string_jx = string_jx.Replace("''", "'','" + cb.Text + "'");
                }

            }
            string str = @"select * from(SELECT   convert(datetime,RQ -2) 日期,XH 序号, null 入库单号,a.CKDH 出库单号, ''类型,  
                          (select TOP 1 mc from zd_ks where bm=KSBM) 来源或去向,YPBM 药品编码,(select mc from zd_yp where bm = ypbm) 药品名称,   JZRY 记账人员, (case when (a.FLAG =19) then '已记账' when (a.FLAG =17) then '已审核' else '未审核' end) 状态,a.BZ 备注, 
                           convert(datetime,JZRQ -2) 记账日期, LYR 领用人, '' 采购人员,BZSL 包装数量,  NCSL 拟出数量, 
                           SL 数量, DJ 单价,JE 金额, PJ 批价, PJJE 批价金额,(select b.mc from zd_yp a,zd_ypjb b where a.jx=b.bm and a.bm=ypbm ) 药品级别,(select b.mc from zd_yp a,zd_ypjx b where a.jx=b.bm and a.bm=ypbm) 剂型 FROM  YK_CKD0 a,YK_CKD1 b
                           where a.ckdh = b.ckdh and rq between " + time1 + " and " + time2 + @"
                           union
                          SELECT  convert(datetime,RQ -2) 日期, XH 序号,   a.RKDH 入库单号,null 出库单号,  (select mc from YK_CRLX where bm=rklx) 类型,
                         (select TOP 1 mc from  yk_zdgys where bm=a.GYSBM) 来源或去向, YPBM 药品编码,(select mc from zd_yp where bm = ypbm) 药品名称,  JZRY 记账人员,   (case when (a.FLAG =19) then '已记账' when (a.FLAG =17) then '已审核' else '未审核' end) 状态,  a.BZ 备注,  
                          convert(datetime,JZRQ -2) 记账日期,'' 领用人, CGRY 采购人员, BZSL 包装数量,'' 拟出数量, 
                          SL 数量,'' 单价,JE 金额,PJ 批价,PJJE 批价金额,(select b.mc from zd_yp a,zd_ypjb b where a.jb=b.bm and a.bm=ypbm ) 药品级别,(select b.mc from zd_yp a,zd_ypjx b where a.jx=b.bm and a.bm=ypbm) 剂型
                          FROM YK_RKD0 a,YK_RKD1 b 
                          where a.rkdh=b.rkdh and rq between " + time1 + " and " + time2 + ") x where "+string_jb+string_jx+"  order by 日期";

            dataGridViewX1.DataSource = db1.sqldt(str);
            
        }

        private void YP_CRKMX_Load(object sender, EventArgs e)
        {
            dateTimePicker1.Value = DateTime.Now.AddDays(-(DateTime.Now.Day - 1));
            buttonItem1_Click(sender,e);

            db db1 = new db();

            int y = 5;
            int x = 6;
            DataTable dt = db1.sqldt("select  mc from ZD_YPJB");
            foreach (DataRow dr in dt.Rows)
            {
                CheckBox cb = new CheckBox();
                cb.Location = new Point(x, y);
                y = y + 23;
                if(y>60)
                {
                    x = x + 90;
                    y = 5;
                }
                cb.Text = dr["mc"].ToString();
                cb.Checked = true; 
                cb.Size = new Size(60, 16);
                panelEx1.Controls.Add(cb);
            }

            int h = 5;
            int j = 6;
            DataTable dt1 = db1.sqldt("select  mc from ZD_YPJX");
            foreach (DataRow dr1 in dt1.Rows)
            {
                CheckBox cb1 = new CheckBox();
                cb1.Location = new Point(j, h);
                h = h + 23;
                if (h > 60)
                {
                    j = j + 90;
                    h = 5;
                }
                cb1.Text = dr1["mc"].ToString();
                cb1.Checked = true;
                cb1.Size = new Size(60, 16);
                panelEx2.Controls.Add(cb1);
            }


        }


        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            string cmdtext4 = "select bm 编码,MC AS 名称 from zd_yp where dh like '" + textBox1.Text.Trim() + "%' or bm like '" + textBox1.Text.Trim() + "%' or mc like '" + textBox1.Text.Trim() + "%'";
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                SqlDataAdapter da1 = new SqlDataAdapter(cmdtext4, cn);
                DataTable dt = new DataTable();
                da1.Fill(dt);
                dataGridViewX3.DataSource = dt;
                dataGridViewX3.Visible = true;
            }
        }

        private void buttonItem3_Click(object sender, EventArgs e)
        {
            db db1 = new db();
            db1.DataToExcel(dataGridViewX1);
        }

        private void buttonItem2_Click(object sender, EventArgs e)
        {
            dgVprint1.Print(dataGridViewX1, true);
        }

        private void textBoxItem4_InputTextChanged(object sender)
        {
            foreach(DataGridViewRow r in dataGridViewX1.Rows )
            {
                if (r.Cells[6].Value.ToString() == textBoxItem4.Text)
                {
                    r.Selected = true;
                }
                else r.Selected = false;
            }
        }

        private void textBoxItem5_InputTextChanged(object sender)
        {
            foreach(DataGridViewRow r2 in dataGridViewX1.Rows )
            {
                if(r2.Cells[2].Value.ToString()==textBoxItem5.Text)
                {
                    r2.Selected=true;
                }
                else r2.Selected=false;
            }
        }

        private void textBoxItem6_InputTextChanged(object sender)
        {
            foreach(DataGridViewRow r3 in dataGridViewX1.Rows )
            {
                if (r3.Cells[1].Value.ToString() == textBoxItem6.Text)
                {
                    r3.Selected = true;
                }
                r3.Selected = false;
            }
        }

        private void dataGridViewX3_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            textBox1.Text = dataGridViewX3.CurrentRow.Cells["名称"].Value.ToString();
            dataGridViewX3.Visible = false;
        }
    }
}